package com.cwt_taxi_xa.receiver;

import android.content.Context;

import com.cwt_taxi_xa.ActivityPower;
import com.cwt_taxi_xa.protocol.CardUtil;
import com.cwt_taxi_xa.protocol.DDUtil;
import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.protocol.NianBaoUtil;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.Tools;


/**
 * @author zr 接收到串口消息
 */
public class SerialDataReceived {

	private static final String TAG = "SerialDataReceived";
	private static StringBuffer dataBuffer = new StringBuffer();
	private static StringBuffer dtu7EBuffer = new StringBuffer();

	/**
	 * 顶灯串口接收数据
	 *
	 * @param buffer
	 * @param size
	 * @param context
	 */
	static byte[] data;
	public static void onDataReceived1(byte[] buffer, int size, Context context) {
		data = new byte[size];
		System.arraycopy(buffer, 0, data, 0, size);
		DDUtil.onDDReceive(data);
		ActivityPower.ddtime = System.currentTimeMillis();//用于判断顶灯是否连接
	}

	/**
	 * 刷卡器接收数据
	 *
	 * @param buffer
	 * @param size
	 * @param context
	 */
	public static void onDataReceived2(byte[] buffer, int size, Context context) {


		CardUtil.onCardReceive(buffer, size);


//		byte[] data = new byte[size];
//		System.arraycopy(buffer, 0, data, 0, size);
//		CardUtil.onCardReceive(data);

	}

	/**
	 * gps模块发来数据
	 *
	 * @param buffer
	 * @param size
	 * @param context
	 */
	public static String gpsstatus;
	public static byte[] gpsbuffer = new byte[1024];
	public static int gpsbufferindex = 0;
	public static void onDataReceived5(byte[] buffer, int size, Context context) {

		if(gpsbufferindex+size>1024){
			String str = new String(gpsbuffer);
			gpsbufferindex = 0;

//			System.out.println(str);
			String[] ss = str.split("\n");
			for (int i = 0; i < ss.length; i++) {
				if(ss[i].contains("$GPTXT,01,01,01,ANTENNA")){
//				System.out.println("---------->"+ss[i]);
					gpsstatus = ss[i];
				}
			}
		}

		System.arraycopy(buffer, 0, gpsbuffer, gpsbufferindex, size);
		gpsbufferindex+=size;

	}

	/**
	 * 计价器串口接收数据
	 *
	 * @param buffer
	 * @param size
	 * @param context
	 */
	static byte[] data_jjq;
	public static void onDataReceived6(byte[] buffer, int size, Context context) {
//		data_jjq = new byte[size];
//		System.arraycopy(buffer, 0, data_jjq, 0, size);
		NianBaoUtil.nbjjq(buffer, size, context);

	}

	/**
	 * 串口接收数据
	 *
	 * @param buffer
	 * @param size
	 * @param context
	 */
	static byte[] data_mcu;
	public static void onDataReceived7(byte[] buffer, int size, Context context) {
		data_mcu = new byte[size];
		System.arraycopy(buffer, 0, data_mcu, 0, size);
		//AppLog.e(TAG,"接收MCU数据："+ Tools.parseByte2HexStr(Tools.byteTobyte(buffer, 0, size)));
		MCUUtil.onCardReceive(data_mcu);
	}


}
